package com.maket.scxx.service.impl;

import java.util.List;

import com.maket.scxx.domain.*;
import com.maket.scxx.feign.GetCodeFeign;
import com.maket.scxx.service.JybzInspectionStandardsAndCpProductService;
import com.maket.scxx.service.JyxmInspectionItemsAndJybzStandardsService;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.maket.scxx.mapper.JybzInspectionStandardMapper;
import com.maket.scxx.service.IJybzInspectionStandardService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 检验标准Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-25
 */
@Service
public class JybzInspectionStandardServiceImpl implements IJybzInspectionStandardService 
{
    @Autowired
    private JybzInspectionStandardMapper jybzInspectionStandardMapper;

    /**
     * 查询检验标准
     * 
     * @param mbCode 检验标准主键
     * @return 检验标准
     */
    @Override
    public JybzInspectionStandard selectJybzInspectionStandardByMbCode(String mbCode)
    {
        return jybzInspectionStandardMapper.selectJybzInspectionStandardByMbCode(mbCode);
    }

    /**
     * 查询检验标准列表
     * 
     * @param jybzInspectionStandard 检验标准
     * @return 检验标准
     */
    @Override
    public List<JybzInspectionStandard> selectJybzInspectionStandardList(JybzInspectionStandard jybzInspectionStandard)
    {
        return jybzInspectionStandardMapper.selectJybzInspectionStandardList(jybzInspectionStandard);
    }

    /**
     * 新增检验标准
     * 
     * @param jybzInspectionStandard 检验标准
     * @return 结果
     */

    @Autowired
    private GetCodeFeign getCodeFeign;
    @Autowired
    private JybzInspectionStandardsAndCpProductService jybzInspectionStandardsAndCpProductService;
    @Autowired
    private JyxmInspectionItemsAndJybzStandardsService jyxmInspectionItemsAndJybzStandardsService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertJybzInspectionStandard(JybzInspectionStandard jybzInspectionStandard)
    {
        String code = getCodeFeign.getUser(17);
        jybzInspectionStandard.setMbCode(code);
        List<JybzInspectionStandardsAndCpProduct> cps = jybzInspectionStandard.getCp();
        List<JyxmInspectionItemsAndJybzStandards> xms = jybzInspectionStandard.getXm();
        if (cps.size() > 0 || xms.size() > 0){
            jybzInspectionStandardMapper.insertJybzInspectionStandard(jybzInspectionStandard);
            int i = 0;
            if (cps.size()>0){
                for (JybzInspectionStandardsAndCpProduct cp : cps) {
                    cp.setMbCode(code);
                    jybzInspectionStandardsAndCpProductService.insertCp(cp);
                    i++;
                }
            }
            if (xms.size()>0){
                for (JyxmInspectionItemsAndJybzStandards xm : xms) {
                    xm.setMbCode(code);
                    jyxmInspectionItemsAndJybzStandardsService.insertXm(xm);
                    i++;
                }
            }
            return i;
        }
        return jybzInspectionStandardMapper.insertJybzInspectionStandard(jybzInspectionStandard);
    }

    /**
     * 修改检验标准
     * 
     * @param jybzInspectionStandard 检验标准
     * @return 结果
     */
    @Override
    public int updateJybzInspectionStandard(JybzInspectionStandard jybzInspectionStandard)
    {
        int i = jybzInspectionStandardsAndCpProductService.deleteByBzCode(jybzInspectionStandard.getMbCode());
        int i1 = jyxmInspectionItemsAndJybzStandardsService.deleteByBzCode(jybzInspectionStandard.getMbCode());
        List<JybzInspectionStandardsAndCpProduct> cps = jybzInspectionStandard.getCp();
        List<JyxmInspectionItemsAndJybzStandards> xms = jybzInspectionStandard.getXm();
        if (cps.size() > 0 || xms.size()>0){
            if (cps.size()>0){
                for (JybzInspectionStandardsAndCpProduct cp : cps) {
                    cp.setMbCode(jybzInspectionStandard.getMbCode());
                    jybzInspectionStandardsAndCpProductService.insertCp(cp);
                }
            }
            if (xms.size()>0){
                for (JyxmInspectionItemsAndJybzStandards xm : xms) {
                    xm.setMbCode(jybzInspectionStandard.getMbCode());
                    jyxmInspectionItemsAndJybzStandardsService.insertXm(xm);
                }
            }
        }
        return jybzInspectionStandardMapper.updateJybzInspectionStandard(jybzInspectionStandard);
    }

    /**
     * 批量删除检验标准
     * 
     * @param mbCodes 需要删除的检验标准主键
     * @return 结果
     */
    @Override
    public int deleteJybzInspectionStandardByMbCodes(String[] mbCodes)
    {
        for (String bzCode : mbCodes) {
            jybzInspectionStandardsAndCpProductService.deleteByBzCode(bzCode);
        }
        for (String mbCode : mbCodes) {
            jyxmInspectionItemsAndJybzStandardsService.deleteByBzCode(mbCode);
        }
        return jybzInspectionStandardMapper.deleteJybzInspectionStandardByMbCodes(mbCodes);
    }

    /**
     * 删除检验标准信息
     * 
     * @param mbCode 检验标准主键
     * @return 结果
     */
    @Override
    public int deleteJybzInspectionStandardByMbCode(String mbCode)
    {
        jybzInspectionStandardsAndCpProductService.deleteByBzCode(mbCode);
        jyxmInspectionItemsAndJybzStandardsService.deleteByBzCode(mbCode);
        return jybzInspectionStandardMapper.deleteJybzInspectionStandardByMbCode(mbCode);
    }
}
